﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace jcc
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        public static string Username { get { return _username; } set { _username = value; } }
        public static string Password { get { return _password; } set { _password = value; } }

        public static int Level { get { return _level; } }

        static string _username, _password;
        static int _level;

        static int _minLevel = 0;
        public Login(string title, int minLevel)
        {
            InitializeComponent();
            lblLoginTitle.Content = title;
            _minLevel = minLevel;
            if (!string.IsNullOrEmpty(jcc.Properties.Settings.Default.Username))
                tbUserName.Text = jcc.Properties.Settings.Default.Username;

            if (jcc.Properties.Settings.Default.bSavePassword)
            {
                if (!string.IsNullOrEmpty(jcc.Properties.Settings.Default.Password))
                    pwbPassword.Password = jcc.Properties.Settings.Default.Password;
            }
        }

        public static readonly RoutedEvent LoginSuccessEvent = EventManager.RegisterRoutedEvent("LoginSuccess", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Login));

        public event RoutedEventHandler LoginSuccess
        {
            add { AddHandler(LoginSuccessEvent, value); }
            remove { RemoveHandler(LoginSuccessEvent, value); }
        }
        /// <summary>
        /// Sets the Password for use in the next TryLogin call
        /// </summary>
        /// <param name="pwd">password to try</param>
        public static void SetPassword(string pwd)
        {
            _password = Password;

        }
        /// <summary>
        /// Sets the Username for use in the next TryLogin call
        /// </summary>
        /// <param name="un">username to try</param>
        public static void SetUsername(string un)
        {
            _username = un;
        }
        private void RaiseLoginSuccessEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(Login.LoginSuccessEvent);
            RaiseEvent(newEventArgs);
        }

        private void bClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        
        public void TryLogin()
        {            
            _level = StandardWebService.WebService.GetAccessLevel(StandardWebService.GetKey(), _username);
            if (_level >= _minLevel)
            {
                this.Hide();
                RaiseLoginSuccessEvent();
            }
            else
            {
                MessageBox.Show("Invalid User ID and/or Password, or your access level is not high enough to use this progam.", "Error Logging in.", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public void bOK_Click(object sender, RoutedEventArgs e)
        {
            _username = tbUserName.Text;
            _password = pwbPassword.Password;

            jcc.Properties.Settings.Default.Username = _username;
            jcc.Properties.Settings.Default.bSavePassword = cbSavePassword.IsChecked.Value;
            if (cbSavePassword.IsChecked.Value)
                jcc.Properties.Settings.Default.Password = _password;
            jcc.Properties.Settings.Default.Save();

            TryLogin();
            
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
